<?php
/**
 * 后台管理员模型
 * Author: lyun
 * Date: 2018/3/23
 * Created by Panxsoft.
 */
namespace App\Models\Admin;

use App\Http\Middleware\AdminAuth;
use App\Models\Concerns\DeSensitive;
use App\Models\Concerns\ScopeFilterable;
use App\Models\Panx\Panx;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject as AuthenticatableUserContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

/**
 * @property int id
 * @property string account
 * @property string email
 * @property string language
 * @property string password
 * @property int is_deleted
 * @property string panx_id
 * @property Panx panx
 *
 * @method static self filterIsSuperMan()
 *
 * @mixin Builder
 */
class PanxAdmin extends Authenticatable implements
    CanResetPasswordContract,
    AuthenticatableUserContract
{
    use HasRoles;
    use ScopeFilterable;
    use DeSensitive;

    const IS_NOT_DELETED = 0;  // 后台管理员启用
    const IS_DELETED = 1;      // 后台管理员禁用
    const GUARD_NAME = 'panx';     // 后台guard

    const SUPER_MAN = '超管';  // 超管的角色
    const FINANCE = '财务';  // 财务的角色

    const ROLE_RELATION = [
        'SUPER_MAN' => self::SUPER_MAN,
        'FINANCE'   => self::FINANCE,
    ];

    protected $guard_name = Guard::ADMIN_PANX;  // 定义laravel-permission使用的 guard

    protected $table = 'panx_admins';

    protected $guarded = [];

    protected $hidden = ['password', 'remember_token', 'reset_token'];

    protected $casts = [
        'id'         => 'int',
        'panx_id'    => 'int',
        'is_deleted' => 'int',
        'phone'      => 'string',
        'sex'        => 'int',
    ];

    /**
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey(); // Eloquent model method
    }

    /**
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function panx()
    {
        return $this->belongsTo(Panx::class, 'panx_id', 'id');
    }

    /**
     * @param $value
     */
    public function setPasswordAttribute($value): void
    {
        if ($value) {
            $this->attributes['password'] = Hash::make($value);
        };
    }

    /**
     * @used-by PanxAdmin::filterIsSuperMan()
     *
     * @param Builder $query
     * @return Builder
     */
    public function scopeFilterIsSuperMan(Builder $query)
    {
        return $query->where('id', '<>', Guard::panxAdmin()->id);
    }

    /**
     * @return bool
     */
    public static function checkIsSuperMan(): bool
    {
        /** @var PanxAdmin $panx_admin */
        $panx_admin = Guard::panxAdmin();

        return $panx_admin->isSuperMan();
    }

    /**
     * @return bool
     */
    public function isSuperMan(): bool
    {
        return $this->hasRole(self::SUPER_MAN);
    }
}
